import os
import re
import time

import openpyxl
from openpyxl.drawing.image import Image

import commen.config
from commen.log import Logger
from tools.draw_pic import draw_multiple_data_picture


def output_acceleration_mutipicture(imu_timestamp, imu_acceleration, time_now):
    save_path = "./picture" + os.sep + f"{time_now}" + 'IMU_acceleration.png'
    y_label_array_list = ['xyz_acceleration']
    title = 'IMU_acceleration'
    legend_config = [['x', 'y', 'z']]
    # zoom_config = {2: (8, 13), 3: (imu_timestamp[-1] - 10 * len(imu_timestamp), imu_timestamp[-1] + 100)}
    # hline_config = {0: 10, 2: 10}
    draw_multiple_data_picture([range(len(imu_timestamp))], [imu_acceleration], title,
                               y_label_array_list, legend_config=legend_config,
                               save_path=save_path)
    return save_path


def ImuDatacontinuity():
    # ---------读取config文件获取筛选字段内容--------------------------
    # key = commen.config.global_config.getRaw('keywords', 'Imubegincontinuation')
    key = 'DataProc:packageData(609) imu'
    # ---------读取config文件获取筛选字段内容--------------------------
    # -----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())

    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "Data" in file:
                aa = os.path.join(root, file)
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path + "imu加速度结果.xlsx"
    sheet.cell(1, 2).value = "log_DataProc系统时间"
    sheet.cell(1, 3).value = "x"
    sheet.cell(1, 4).value = "y"
    sheet.cell(1, 5).value = "z"

    row = 1
    logger = Logger(logger='TestMylog').getlog()
    fm = open(aa, 'r', encoding='UTF-8')  # 打开目标文件
    logger.info("打开目标文件")
    lines = fm.readlines()
    logger.info("获取内容")
    num = 0
    log_DataProctime_list = []
    x_accelerometer = []
    y_accelerometer = []
    z_accelerometer = []
    logger.info("开始逐行读取")
    disc = {}
    for lines in lines:  # 对内容 进行逐行读取

        if key in lines:  # kye是读取config文件获取的筛选字段
            num = num + 1
            bbxitongtime = lines[:lines.index("<thread")]  # 根据关键字“log_mcucomm”截取前面的系统时间戳
            # e = lines[lines.rfind(key):lines.rfind('(0)')]
            # e = e[e.rfind(key):e.rfind('(42)')]
            # e = e[e.rfind(key):e.rfind('(10)')]
            # e = e[e.rfind(key):e.rfind('(52)')]
            # aatime = e.split(key)
            # imutime_list.append(aatime[1])
            # aa = lines.split("=>")  # 截取数据=>末尾的字符串时间戳
            # try:
            #     list.append(aa[1])  # 追加到list中
            # except:
            #     print(1)
            x, y, z = re.findall(r'x=(.*?) y=(.*?) z=(.*?) sample', lines)[0]
            x_accelerometer.append(x)
            y_accelerometer.append(y)
            z_accelerometer.append(z)
            log_DataProctime_list.append(bbxitongtime)
            sheet.cell(row, 2).value = bbxitongtime
            sheet.cell(row, 3).value = x
            sheet.cell(row, 4).value = y
            sheet.cell(row, 5).value = z
            row += 1
    picture_path = output_acceleration_mutipicture(log_DataProctime_list,
                                                   [x_accelerometer, y_accelerometer, z_accelerometer], time_now)
    print(disc)
    (sheet2 := book.create_sheet(index=1)).add_image(Image(picture_path))
    sheet2.title = 'imu连加速度趋势图'
    # ----------------------将对比的数据写入excel---------------------------------
    book.save(saveExcel)
    # ----------------------将对比的数据写入excel---------------------------------


ImuDatacontinuity()
